package com.util;

import java.sql.*;
import javax.naming.*;



import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;


public class Conectar {

    private static final String driver = "com.mysql.jdbc.Driver";// = "COM.cloudscape.core.JDBCDriver";
    private static final String driver2 = "org.gjt.mm.mysql.Driver";
    //private static final String protocol1 = "jdbc:mysql://localhost/pruebasgecofuensa";
    private static final String protocol1 = "jdbc:mysql://127.0.0.1/GeCoFuensa";
    //private static final String protocol2 = "jdbc:mysql://localhost/";
    
    
        
    private static final String usuario="root";
    private static final String password = "03885536";

    private Conectar() {

    }
    /**
     * Devuelve una conexion a la base de datos, para poder hacer consultas, ...
     * @return Un objeto Connection para conectar con la base de datos ya definida en la clase.
     * @throws javax.naming.NamingException
     * @throws java.sql.SQLException
     */
    public static Connection getConnection() throws NamingException, SQLException {
        /*if (driver == null || protocol1 == null) {
            throw new SQLException("Driver not specified.");
        }*/
        System.out.println("Nueva conexion");
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("03885536");
        dataSource.setDatabaseName("GeCoFuensa");
        dataSource.setServerName("localhost");

        Connection conexion = dataSource.getConnection();
        return conexion;


        /*
        System.out.println("Conector");
        try {
            System.out.println("Driver 1");
            Class.forName(driver).newInstance();
            System.out.println("Driver 2");
            Connection connection = DriverManager.getConnection(protocol1, usuario, password);
            System.out.println("Driver 3");
            return connection;
        } catch (InstantiationException ex) {
            System.out.println("error 1");
            Logger.getLogger(Conectar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            System.out.println("error 2");
            Logger.getLogger(Conectar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException cnfe) {
            System.out.println("Excepcion Driver");
            System.out.println("Error excepcion: "+cnfe.getMessage());
            cnfe.printStackTrace();
            throw new SQLException("Driver <" + driver + "> not found in the classpath.");
        }
        return null;
         */

    }
}
